package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * Get_Response类
 * 实现获取响应的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class Get_Response implements AxdrType {

    private byte[] dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private GetResponseNormal getResponseNormal = null;
    private GetResponseNormalList getResponseNormalList = null;
    private GetResponseRecord getResponseRecord = null;

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        GET_RESPONSE_NORMAL(1),
        GET_RESPONSE_NORMAL_LIST(2),
        GET_RESPONSE_RECORD(3),
        GET_RESPONSE_RECORD_LIST(4),
        GET_RESPONSE_NEXT(5);

        private final int value;

        Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice fromValue(int value) {
            for (Choice choice : Choice.values()) {
                if (choice.value == value) {
                    return choice;
                }
            }
            return _ERR_NONE_SELECTED;
        }
    }

    public Get_Response() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setGetResponseNormal(GetResponseNormal getResponseNormal) {
        this.resetChoices();
        this.choice = Choice.GET_RESPONSE_NORMAL;
        this.getResponseNormal = getResponseNormal;
    }

    public void setGetResponseNormalList(GetResponseNormalList getResponseNormalList) {
        this.resetChoices();
        this.choice = Choice.GET_RESPONSE_NORMAL_LIST;
        this.getResponseNormalList = getResponseNormalList;
    }

    public void setGetResponseRecord(GetResponseRecord getResponseRecord) {
        this.resetChoices();
        this.choice = Choice.GET_RESPONSE_RECORD;
        this.getResponseRecord = getResponseRecord;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        com.dl698.asn1.axdr.AxdrEnum choosen = new com.dl698.asn1.axdr.AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = Choice.fromValue((int) choosen.getValue());

        if (this.choice == Choice.GET_RESPONSE_NORMAL) {
            this.getResponseNormal = new GetResponseNormal();
            codeLength += this.getResponseNormal.decode(input);
            return codeLength;
        } else if (this.choice == Choice.GET_RESPONSE_NORMAL_LIST) {
            this.getResponseNormalList = new GetResponseNormalList();
            codeLength += this.getResponseNormalList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.GET_RESPONSE_RECORD) {
            this.getResponseRecord = new GetResponseRecord();
            codeLength += this.getResponseRecord.decode(input);
            return codeLength;
        } else if (this.choice == Choice.GET_RESPONSE_RECORD_LIST) {
            // this.getResponseRecordList = new GetResponseRecordList();
            // codeLength += this.getResponseRecordList.decode(input);
            // return codeLength;
        } else if (this.choice == Choice.GET_RESPONSE_NEXT) {
            // this.getResponseNext = new GetResponseNext();
            // codeLength += this.getResponseNext.decode(input);
            // return codeLength;
        }
        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            try {
                for (int i = this.dataCode.length - 1; i >= 0; i--) {
                    output.write(this.dataCode[i]);
                }
            } catch (java.io.IOException e) {
                throw new RuntimeException("Error writing dataCode to output stream", e);
            }
            return this.dataCode.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == Choice.GET_RESPONSE_NORMAL) {
            if (this.getResponseNormal != null) {
                codeLength += this.getResponseNormal.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("getResponseNormal is null");
            }
        } else if (this.choice == Choice.GET_RESPONSE_NORMAL_LIST) {
            if (this.getResponseNormalList != null) {
                codeLength += this.getResponseNormalList.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("getResponseNormalList is null");
            }
        } else if (this.choice == Choice.GET_RESPONSE_RECORD) {
            if (this.getResponseRecord != null) {
                codeLength += this.getResponseRecord.encode(output);
                com.dl698.asn1.axdr.AxdrEnum c = new com.dl698.asn1.axdr.AxdrEnum();
                c.setMinMaxVal(3);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("getResponseRecord is null");
            }
        } else if (this.choice == Choice.GET_RESPONSE_RECORD_LIST) {
            // 简化版，暂时不支持
            throw new RuntimeException("GET_RESPONSE_RECORD_LIST is not supported in this simplified version");
        } else if (this.choice == Choice.GET_RESPONSE_NEXT) {
            // 简化版，暂时不支持
            throw new RuntimeException("GET_RESPONSE_NEXT is not supported in this simplified version");
        }

        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding Get_Response", e);
        }
        this.dataCode = revOStream.getArray();
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public GetResponseNormal getGetResponseNormal() {
        return this.getResponseNormal;
    }

    public GetResponseNormalList getGetResponseNormalList() {
        return this.getResponseNormalList;
    }

    public GetResponseRecord getGetResponseRecord() {
        return this.getResponseRecord;
    }

    // 简化版，只包含已存在的类
    // public GetResponseRecordList getGetResponseRecordList() {
    // return this.getResponseRecordList;
    // }

    // public GetResponseNext getGetResponseNext() {
    // return this.getResponseNext;
    // }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.getResponseNormal = null;
        this.getResponseNormalList = null;
        this.getResponseRecord = null;
        // 简化版，只包含已存在的类
        // this.getResponseRecordList = null;
        // this.getResponseNext = null;
    }

    @Override
    public String toString() {
        if (this.choice == Choice.GET_RESPONSE_NORMAL) {
            if (this.getResponseNormal != null) {
                return "choice: {get_response_normal: " + this.getResponseNormal + "}";
            } else {
                return "choice is get_response_normal but getResponseNormal is null";
            }
        } else if (this.choice == Choice.GET_RESPONSE_NORMAL_LIST) {
            if (this.getResponseNormalList != null) {
                return "choice: {get_response_normal_list: " + this.getResponseNormalList + "}";
            } else {
                return "choice is get_response_normal_list but getResponseNormalList is null";
            }
        } else if (this.choice == Choice.GET_RESPONSE_RECORD) {
            if (this.getResponseRecord != null) {
                return "choice: {get_response_record: " + this.getResponseRecord + "}";
            } else {
                return "choice is get_response_record but getResponseRecord is null";
            }
        } else if (this.choice == Choice.GET_RESPONSE_RECORD_LIST) {
            return "choice is get_response_record_list but this class is simplified";
        } else if (this.choice == Choice.GET_RESPONSE_NEXT) {
            return "choice is get_response_next but this class is simplified";
        } else {
            return "unknown";
        }
    }
}